package net.chenlin.dp.modules.business.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.chenlin.dp.common.entity.Page;
import net.chenlin.dp.common.entity.R;
import net.chenlin.dp.common.utils.ShiroUtils;
import net.chenlin.dp.modules.business.entity.dto.DistrictDto;
import net.chenlin.dp.modules.business.entity.vo.districtManage.DistrictManageListVo;
import net.chenlin.dp.modules.business.service.DistrictManageService;
import net.chenlin.dp.modules.sys.controller.AbstractController;
import net.chenlin.dp.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Yangsm
 * @ClassName: DistrictManageController
 * @Description: 台区管理
 * @date 2023年05月01日 16:42
 */
@Slf4j
@RestController
@RequestMapping("/district")
@Api(tags = "台区管理")
public class DistrictManageController extends AbstractController {

    @Autowired
    private DistrictManageService districtManageService;

    @RequestMapping(value = "/getDeviceList", method = RequestMethod.POST)
    @ApiOperation(value = "台区列表")
    public Page<DistrictManageListVo> getDistrictList(@RequestBody Map<String, Object> params) {
        String id = (String) ShiroUtils.getSession().getId();
        SysUserEntity userInfoBySessionId = ShiroUtils.getUserInfoBySessionId(id);
        System.out.println(userInfoBySessionId);
        //SysUserEntity userEntity = ShiroUtils.getUserEntity();
        //System.out.println(" ==== "+userEntity);
        SysUserEntity user = getUser();
        System.out.println(user);
        if(null == user){
            log.info("未获取到当前登录用户！");
            return new Page<>();
        }
        Long orgId = getUser().getOrgId();
        if(orgId != 1){
            params.put("orgId",orgId);
        }
        return districtManageService.getDistrictList(params);
    }

    @RequestMapping(value = "/createDistrict", method = RequestMethod.POST)
    @ApiOperation(value = "创建台区信息")
    public R createDistrict(@RequestBody DistrictDto dto) {
        SysUserEntity user = getUser();
        if(null == user){
            return R.error("未获取到当前登录用户！");
        }
        dto.setCreateId(user.getUserId());
        dto.setOrgId(user.getOrgId());
        return districtManageService.createDistrict(dto);
    }

    @RequestMapping(value = "/getTree", method = RequestMethod.GET)
    @ApiOperation(value = "获取台区树")
    public R getTree(Long districtId) {
        return districtManageService.getTree(districtId);
    }

    @RequestMapping(value = "/updateDistrict", method = RequestMethod.POST)
    @ApiOperation(value = "修改台区信息")
    public R updateDistrict(@RequestBody DistrictDto dto) {
        SysUserEntity user = getUser();
        if(null == user){
            return R.error("未获取到当前登录用户！");
        }
        dto.setCreateId(user.getUserId());
        dto.setOrgId(user.getOrgId());
        return districtManageService.updateDistrict(dto);
    }

    @RequestMapping(value = "/deleteDistrict", method = RequestMethod.GET)
    @ApiOperation(value = "删除台区信息")
    public R deleteDistrict(Long districtId) {
        return districtManageService.deleteDistrict(districtId);
    }

}
